package com.ibeeking.found.cms.b.rest.service.impl;

import com.ibeeking.found.cms.b.rest.service.IPageSystemBaseService;
import com.ibeeking.found.cms.service.common.dos.PageSystemBaseDO;
import com.ibeeking.found.cms.service.common.query.EffectiveByTypePageQuery;
import com.ibeeking.found.cms.service.common.query.PageBaseQuery;
import com.ibeeking.found.cms.service.mapper.PageSystemBaseMapper;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.nematos.utils.data.Collections3;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName PageBaseServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-05-08 16:05
 **/
@Service
public class PageSystemBaseServiceImpl extends ServiceImpl<PageSystemBaseMapper, PageSystemBaseDO> implements IPageSystemBaseService {

    @Resource
    private PageSystemBaseMapper pageSystemBaseMapper;

    @Resource
    private FileOssProp fileOssProp;


    @Override
    public Page<PageSystemBaseDO> pageList(EffectiveByTypePageQuery query) {
        LambdaQueryWrapper<PageSystemBaseDO> queryWrapper = new LambdaQueryWrapper<PageSystemBaseDO>();
        queryWrapper.eq(query.getPlatform() != null, PageSystemBaseDO::getPlatform, query.getPlatform());
        queryWrapper.eq(query.getType() != null, PageSystemBaseDO::getType, query.getType());
        queryWrapper.eq(query.getPublishStatus() != null, PageSystemBaseDO::getPublishStatus, query.getPublishStatus());
        Page<PageSystemBaseDO> pageSystemBaseDOPage = pageSystemBaseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), queryWrapper);
        List<PageSystemBaseDO> records = pageSystemBaseDOPage.getRecords();
        if (Collections3.isNotEmpty(records)) {
            records.forEach(x -> {
                if(StringUtils.isNotEmpty(x.getImgUrl())) {
                    x.setImgUrl(UrlUtils.addUrlDomainName(fileOssProp.getHost(), x.getImgUrl()));
                }
            });
        }
        return pageSystemBaseDOPage;
    }


    @Override
    public List<PageSystemBaseDO> list(PageBaseQuery pageBaseQuery) {
        return pageSystemBaseMapper.selectList(new LambdaQueryWrapper<PageSystemBaseDO>()
                .in(Collections3.isNotEmpty(pageBaseQuery.getIds()), PageSystemBaseDO::getId, pageBaseQuery.getIds())
        );
    }

    @Override
    public List<PageSystemBaseDO> list() {
        return pageSystemBaseMapper.selectList(new LambdaQueryWrapper<>());
    }
}